﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication2
{
    public static class MyExtensionMethods
    {
        public static string Excerpt(this string str, int length = 30)
        {
            if (str.Length < length)
                return str;

            return str.Substring(0, length) + "...";
        }
     
        public static string PrettyPassedTime(this DateTime date)
        {
        //http://fluentdatetime.codeplex.com/
            var ts = new TimeSpan(DateTime.Now.Ticks - date.Ticks);
            var delta = (int)Math.Floor(ts.TotalSeconds);

            //var ts = date.Subtract(DateTime.Now);

            //var delta = ts.TotalSeconds;

            const int SECOND = 1;
            const int MINUTE = 60 * SECOND;
            const int HOUR = 60 * MINUTE;
            const int DAY = 24 * HOUR;
            const int MONTH = 30 * DAY;

            if (delta <= 0)
            {
                return "now";
            }
            if (delta < 1 * MINUTE)
            {
                return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
            }
            if (delta < 2 * MINUTE)
            {
                return "a minute ago";
            }
            if (delta < 45 * MINUTE)
            {
                return ts.Minutes + " minutes ago";
            }
            if (delta < 90 * MINUTE)
            {
                return "an hour ago";
            }
            if (delta < 24 * HOUR)
            {
                return ts.Hours + " hours ago";
            }
            if (delta < 48 * HOUR)
            {
                return "yesterday";
            }
            if (delta < 30 * DAY)
            {
                return ts.Days + " days ago";
            }
            if (delta < 12 * MONTH)
            {
                int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
                return months <= 1 ? "one month ago" : months + " months ago";
            }
            else
            {
                int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
                return years <= 1 ? "one year ago" : years + " years ago";
            }
        }

        //public static string Pluralize(this string str, bool plural)
        //{
        //    if (plural) return str + "s";
        //    return str;
        //}

        ///// <summary>
        ///// Calcola tempo passato e stampa formato tipo
        ///// 1 month
        ///// 2 days 
        ///// 3 min ago
        ///// </summary>
        //public static string FormatTimeAgo(this DateTime date, DateTime rifDate)
        //{
        //    var diff = date.Subtract(rifDate);

        //    var pluralS = false;
        //    var unitTime = string.Empty;
        //    var qtyUnitTime = 0;

        //    /*
        //     * Comincio con unita più grandi fino a scendere
        //     */
        //    if (diff.TotalDays > 730)
        //    {
        //        pluralS = true;
        //        unitTime = "y";
        //        qtyUnitTime = (int)Math.Floor(diff.TotalDays / 360);
        //    }
        //    else if (diff.TotalDays > 730)
        //    {
        //        pluralS = true;
        //        unitTime = "y";
        //        qtyUnitTime = (int)Math.Floor(diff.TotalDays / 360);
        //    }
        //    if (diff.TotalDays > 60)
        //    {
        //        pluralS = true;
        //        unitTime = "m";
        //        qtyUnitTime = (int)Math.Floor(diff.TotalDays / 30);
        //    }
        //    if (diff.TotalDays < 60 && diff.TotalDays > 30)
        //    {
        //        pluralS = true;
        //        unitTime = "m";
        //        qtyUnitTime = (int)Math.Floor(diff.TotalDays / 30);
        //    }

        //    return string.Format("{0} {1} ago..", qtyUnitTime, GetUnityTimeName(pluralS, unitTime));
        //}

        //private static string GetUnityTimeName(bool plural, string unitTime)
        //{
        //    switch (unitTime)
        //    {
        //        case "y":
        //            {
        //                return "year".Pluralize(plural);
        //            } break;

        //        case "m":
        //            {
        //                return "month".Pluralize(plural);
        //            } break;

        //        case "d":
        //            {
        //                return "day".Pluralize(plural);
        //            } break;

        //        case "h":
        //            {
        //                return "hour".Pluralize(plural);
        //            } break;

        //        case "s":
        //            {
        //                return "second".Pluralize(plural);
        //            } break;
        //    }

        //    return string.Empty;
        //}
    }
}